package com.example.peanutsubway.service.impl;


import com.example.peanutsubway.dao.MenuInfoDao;
import com.example.peanutsubway.dao.impl.MenuInfoDaoImpl;
import com.example.peanutsubway.entity.Menu;
import com.example.peanutsubway.entity.MenuInfo;
import com.example.peanutsubway.service.MenuInfoService;
import com.example.peanutsubway.service.abst.MenuInfoServiceAbst;
import com.example.peanutsubway.util.PageInfo;
import com.example.peanutsubway.vo.MenuType;

import java.sql.Connection;
import java.util.List;

/**
 * @Author：liulei
 * @Version：1.0
 * @Date：2021/10/11-17:55
 * @Since:jdk1.8
 * @Description:
 */
public class MenuInfoServiceImpl extends MenuInfoServiceAbst implements MenuInfoService {
    private MenuInfoDao menuInfoDao = new MenuInfoDaoImpl();

    @Override
    public void setConnection(Connection connection) {
        menuInfoDao.setConnection(connection);
    }

    @Override
    public List<MenuInfo> showUserMenuByRoleId(Integer roleId) {
        // 查询一级菜单
        List<MenuInfo> parentMenu = menuInfoDao.findParentMenu(roleId);
        // 遍历
        for (MenuInfo menu : parentMenu) {
            List<MenuInfo> sonMenu = menuInfoDao.findSonMenu(roleId, menu.getMid());
            if (sonMenu != null && sonMenu.size() > 0) {
                menu.setSonList(sonMenu);
            }
        }
        return parentMenu;
    }

    @Override
    public String[] showUserMenuByRoleMinId(Integer roleId) {
        List<MenuInfo> menuInfos = new MenuInfoDaoImpl().showUserMenuByRoleMinId(roleId);
        String[] treeChecked = new String[menuInfos.size()];
        for (int i = 0; i < menuInfos.size(); i++) {
            treeChecked[i] = String.valueOf(menuInfos.get(i).getMid());
        }
        return treeChecked;
    }

    @Override
    public Boolean addTree(Integer menuId,Integer rid) {
        return menuInfoDao.addTree(menuId,rid);
    }

    @Override
    public PageInfo findAllMenu(Menu menu, String nowPage, String pageNum) {
        PageInfo pageInfo = new PageInfo(nowPage, pageNum,menuInfoDao.count(menu));
        List<Menu> allMenu = menuInfoDao.findAllMenu(menu, pageInfo);
        pageInfo.setDataList(allMenu);
        return pageInfo;
    }

    @Override
    public List<MenuType> findAllMenuType() {
        return menuInfoDao.findAllMenuType();
    }

    @Override
    public Integer lastTreeId() {
        return menuInfoDao.lastTreeId();
    }

    @Override
    public Boolean updateMenu(Menu menu) {
        return menuInfoDao.updateMenu(menu);
    }

    @Override
    public Boolean addMenu(Menu menu) {
        return menuInfoDao.addMenu(menu);
    }

    @Override
    public Boolean deleteMenu(Integer id){
        return menuInfoDao.deleteMenu(id);
    }
}
